package com.example.xx.controller;



import com.example.xx.pojo.Category;
import com.example.xx.service.CategoryService;
import com.example.xx.util.Code;
import com.example.xx.util.PageUtil;
import com.example.xx.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@ResponseBody
@RequestMapping
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    //查询
    @RequestMapping("/fenglei")
    public Result fenglei(@RequestBody PageUtil pageUtil){
        PageInfo<Category> cha = categoryService.cha(pageUtil);

        if (cha != null){
            System.out.println(cha);
            return new Result(Code.GET_OK,cha,"查询成功");
        }else {
            return new Result(Code.GET_ERR,"查询失败");
        }
    }

    //假删除
    @RequestMapping("/shanchu/{id}")
    public Result shanchu(@PathVariable("id")Integer id){
        System.out.println("id是"+id);
        int shanchu = categoryService.shanchu(id);
        if (shanchu > 0){
            return new Result(Code.DELETE_OK,"删除成功");
        }else {
            return new Result(Code.DELETE_ERR,"删除失败");
        }
    }

    //添加
    @RequestMapping("/tianjia")
    public Result tianjia(@RequestBody Category category){
        System.out.println(category);
        int tianjia = categoryService.tianjia(category);
        if (tianjia > 0){
            return new Result(Code.GET_OK,"添加成功");
        }else {
            return new Result(Code.GET_ERR,"添加失败");
        }
    }
    //修改
    @RequestMapping("/xiugai1")
    public Result xiugai(@RequestBody Category category){
        System.out.println(category);
        int update = categoryService.update(category);
        if (update > 0){
            return new Result(Code.GET_OK,"修改成功");
        }else {
            return new Result(Code.GET_ERR,"修改失败");
        }
    }
    //查询全部
    @RequestMapping("/chaquanbiao/{id}")
    public Result chaquanbiao(@PathVariable("id")Integer id){
        System.out.println("id是"+id);
        List<Category> chaquanbiao = categoryService.chaquanbiao(id);
        if (chaquanbiao != null){
            return new Result(Code.GET_OK,chaquanbiao,"查询成功");
        }else {
            return new Result(Code.GET_ERR,"查询失败");
        }
    }
}
